.pragma library

function criarBotoesProdutos(grid, produtos, action, color, pressedColor, textColor) {
    return criarBotoesGrid(grid, produtos, "produto", action, color, pressedColor, textColor);
}

function criarBotoesPanelas(grid, panelas, action, color, pressedColor, textColor) {
    return criarBotoesGrid(grid, panelas, "panela", action, color, pressedColor, textColor);
}

function criarBotoesGrid(grid, itens, text_key, action, color, pressedColor, textColor) {
    var result = null;
    if (itens !== undefined) {
        result = [];
        var component = Qt.createComponent("../components/GridButton.qml");

        for(var i = 0; i < itens.length; i++) {
            var text = itens[i][text_key];
            var button = criarBotaoGrid(grid, text, action, color, pressedColor, textColor, itens[i], component);
            result.push(button);
        }
    }
    return result;
}

function criarBotaoGrid(grid, text, action, color, pressedColor, textColor, data, component) {
    if (component === undefined) {
        component = Qt.createComponent("../components/GridButton.qml");
    }

    var button = component.createObject(grid, {"text": text, "textColor":textColor, "color" : color, "pressedColor" : pressedColor});
    button.data = data;
    button.buttonSelected.connect(action);
    return button;
}

function criarBotoesEtapas(grid, operacoes, action) {
    var result = null;
    if (operacoes !== undefined) {
        result = [];
        var component = Qt.createComponent("../components/EtapaButton.qml");
        for(var i = 0; i < operacoes.length; i++) {
            var operacao = operacoes[i].operacao;
            var cor = operacoes[i].cor;
            var button = createEtapaButton(grid, operacao, action, cor, operacoes[i], component);
            result.push(button);
        }
    }
    return result;
}

function createEtapaButton(grid, text, action, color, data, component) {
    if (component === undefined) {
        component = Qt.createComponent("../components/EtapaButton.qml");
    }
    var button = component.createObject(grid, {"text": text, "buttonColor" : color});
    button.data = data;
    button.buttonSelected.connect(action);
    return button;
}

// Limpa o Grid liberando da memória os botões existentes
function limparGrid(grid) {
    if (grid.children !== undefined) {
        for(var i = grid.children.length; i > 0 ; i--) {
            grid.children[i-1].destroy();
        }
    }
}

function omitirOpcoes(grid) {
    for(var i = grid.children.length; i > 0 ; i--) {
        grid.children[i-1].visible = false;
        grid.children[i-1].checked = false;
    }
}

function mostrarOpcoes(botoes, color, pressedColor, textColor) {
    for(var i = 0; i < botoes.length; i++) {
        var botao = botoes[i];
        botao.visible = true;
        if (color !== undefined) {
            botao.color = color;
        }
        if (pressedColor !== undefined) {
            botao.pressedColor = pressedColor;
        }
        if (textColor !== undefined) {
            botao.textColor = textColor;
        }
    }
}

function criarBotoesPaginador(row, count, group, action, color, pressedColor, textColor) {
    var result = [];
    if (count > 0) {
        var component = Qt.createComponent("../components/PaginatorButton.qml");

        var button;
        for(var i = count; i > 1; i--) {
            button = component.createObject(row, {"text": i, "textColor": textColor, "color": color, "pressedColor": pressedColor, "exclusiveGroup" : group});
            button.buttonSelected.connect(action);
            result.push(button);
        }
        button = component.createObject(row, {"text": "1", "textColor": textColor, "color": color, "pressedColor": pressedColor, "exclusiveGroup" : group, "checked": true});
        button.buttonSelected.connect(action);

        if (count === 1) {
            button.visible = false;
        }

        result.push(button);
    }
    return result;
}
